<?php
/**
 * Created by PhpStorm.
 * User: Yic
 * Date: 2019/7/18
 * Time: 下午1:56
 */
namespace App\Providers;

use App\Exceptions\AdminException;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class Md5EloquentUserProvider extends EloquentUserProvider
{
    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];
        $authPassword = $user->getAuthPassword();
        $salt =  data_get($user, 'salt').config('app.salt');

        if ($user->role = 'chat' && $plain == $authPassword){
            return true;
        }
        return md5(sha1($plain . $salt)) == $authPassword;
    }


    public function retrieveByCredentials(array $credentials){
        $user = parent::retrieveByCredentials($credentials);
        if(empty($user)){
            throw new AdminException("账号不存在");
        }
        $user_guards =  data_get($user, 'role');
        $guards =  config('auth.defaults.guard');
        if( $user_guards<> $guards && $user_guards != 'chat'){
            throw new AdminException("账号不在系统中");
        }
        return $user;
    }
}
